<?php
	$sg = $_GET['sg'];
	$key = $_GET['key'];
	$state = $_GET['st'];
	$city = $_GET['cty'];
	$priceFrom = $_GET['pf'];
	$priceTo = $_GET['pt'];
	
	if(!empty($_GET['sg'])) 
	{	
	
	 if(is_numeric($state))
		 $state = getStateName($state);
	 if(is_numeric($city))
		$city = getCityName($city);
		
	 if($city!="" || $state!="")
		  if($city!="" && $state!="") 
			$searchFor=$state." - ".$city;
		  else if($state!="")
		   	$searchFor=$state;
		  else if($city!="")
		   		$searchFor=$city;

		$sqlSelPro="SELECT * FROM tbl_product ";	
		$searchIncludes="";														
		if($city!="" || $state!="") 
		{// City & state search starts
		  $searchIncludes="location";
		  if($city!="" && $state!="") 
			$sqlSelPro .= "LEFT JOIN tbl_users ON(tbl_product.posted_by = tbl_users.user_id)						
							WHERE tbl_users.state LIKE '%$state%' AND tbl_users.city LIKE '%$city%'";
		  else if($state!="")
		   		$sqlSelPro .= "LEFT JOIN tbl_users ON(tbl_product.posted_by = tbl_users.user_id)						
							WHERE tbl_users.state LIKE '%$state%'";
		  else if($city!="")
		   		$sqlSelPro .= "LEFT JOIN tbl_users ON(tbl_product.posted_by = tbl_users.user_id)						
							WHERE tbl_users.city LIKE '%$city%'";					
			if(($priceFrom!="" && is_numeric($priceFrom)) || ($priceTo!=""  && is_numeric($priceTo)))
			 {// Price range including City & state search starts
				if($priceFrom!="" && $priceTo!="" && is_numeric($priceFrom) && is_numeric($priceTo))
					$sqlSelPro .= " AND tbl_product.product_price BETWEEN $priceFrom AND $priceTo ";
				else if($priceFrom!="" && is_numeric($priceFrom))
			        $sqlSelPro .= " AND tbl_product.product_price>=$priceFrom ";  
				else if($priceTo!="" && is_numeric($priceTo))
			        $sqlSelPro .= " AND tbl_product.product_price<=$priceTo ";
		}// Price range including City & state search Ends	
		}// City & state search Ends
	    else if(($priceFrom!="" && is_numeric($priceFrom)) || ($priceTo!=""  && is_numeric($priceTo)))
		{// Price range search alone starts
		     $searchIncludes="pricerange";
		     if($priceFrom!="" && $priceTo!="" && is_numeric($priceFrom) && is_numeric($priceTo))
			   $sqlSelPro .= "WHERE product_price BETWEEN $priceFrom AND $priceTo ";
			 else if($priceFrom!="" && is_numeric($priceFrom))
			       $sqlSelPro .= "WHERE product_price>=$priceFrom ";  
			 else if($priceTo!="" && is_numeric($priceTo))
			        $sqlSelPro .= "WHERE product_price<=$priceTo ";
		}// Price range search alone Ends
		if($_GET['sg']=='online')
		{
		 if($searchIncludes=="location")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND tbl_product.product_name LIKE '%$key%' AND tbl_product.selling_type='online'"; 
		   else
		     $sqlSelPro.=" AND tbl_product.selling_type='online'";
		 }
		 else if($searchIncludes=="pricerange")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND product_name LIKE '%$key%' AND selling_type='online'"; 
		   else
		     $sqlSelPro.=" AND selling_type='online'";
		  }
		  else
		  {
		    if($key!='')
		     $sqlSelPro.=" WHERE product_name LIKE '%$key%' AND selling_type='online'"; 
		   else
		     $sqlSelPro.=" WHERE selling_type='online'";
		  }	
		 }
		 if($_GET['sg']=='local')
		{
		 if($searchIncludes=="location")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND tbl_product.product_name LIKE '%$key%' AND tbl_product.selling_type='local'"; 
		   else
		     $sqlSelPro.=" AND tbl_product.selling_type='local'";
		 }
		 else if($searchIncludes=="pricerange")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND product_name LIKE '%$key%' AND selling_type='local'"; 
		   else
		     $sqlSelPro.=" AND selling_type='local'";
		  }
		  else
		  {
		    if($key!='')
		     $sqlSelPro.=" WHERE product_name LIKE '%$key%' AND selling_type='local'"; 
		   else
		     $sqlSelPro.=" WHERE selling_type='local'";
		  }	
		 }
		 if($_GET['sg']=='all')
		{
		 if($searchIncludes=="location")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND tbl_product.product_name LIKE '%$key%'"; 
		 }
		 else if($searchIncludes=="pricerange")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND product_name LIKE '%$key%'"; 
		  }
		  else
		    if($key!='')
		     $sqlSelPro.=" WHERE product_name LIKE '%$key%'"; 
		 }
		/* else
		{
		$sqlSelPro="SELECT * FROM tbl_product";
		$pagelink="srch&sg";
	} */
  }
	$pLink = "srch&sg=$sg&key=$key&st=$state&cty=$city&pf=$priceFrom&pt=$priceTo";	
	
	$pos     = strripos($sqlSelPro, "where");
	$curDate = date("Y-m-d G:i:s",time());
	//echo $curDate;
	if ($pos === false) {
		//$sqlSelPro.=" WHERE bargain_duration >= '$curDate' && posted_by!='$userId'";
		$sqlSelPro.=" WHERE posted_by!='$userId'";
	} else {
		//echo "found"; posted_by!='$userId'
		//$sqlSelPro.= " AND bargain_duration >= '$curDate'";
		$sqlSelPro.= " AND posted_by!='$userId'";
	}
	//echo $sqlSelPro;
?>	

<?php 		   
if(!empty($_GET['sortby'])) {
	if($_GET['sortby']=='lps') {
		$sqlSelPro .= " order by tbl_product.product_price ASC, tbl_product.shipping ASC";		
	}
	else if($_GET['sortby']=='hps') {
		$sqlSelPro .= " order by tbl_product.product_price DESC, tbl_product.shipping DESC";		
	}
	else if($_GET['sortby']=='bm') {
		$sqlSelPro .= " order by field(tbl_product.product_name, '$key') DESC";		
	}
	else if($_GET['sortby']=='gen') {
		$sqlSelPro .= " order by tbl_product.product_id ASC";		
	}
	else if($_GET['sortby']=='local') {
		$sqlSelPro .= " order by field(tbl_product.selling_type, 'local') desc";		
	}
}		

//echo $sqlSelPro;

$rstSelPro=mysql_query(getPagingQuery($sqlSelPro,$itemPerPage));			  
$num=mysql_num_rows($rstSelPro);				   
if($num==0) {					
	echo "No Products Found...";
 
}
else { ?>
	
   <br>
   <table width='670' border='0' align='center' cellspacing='0'>
		   <tr>
			<td colspan='6'>
				
				<table align="center" width="100%" border="0">
					<tr>
						<td width="8%">Sort by:</td>
						<td width="12%"><a href="index.php?addProduct&<?php echo $pLink; ?>&sortby=bm">Best Match</a></td>
						<td width="22%"><a href="index.php?addProduct&<?php echo $pLink; ?>&sortby=lps">Lowest Price &amp; Ship</a></td>
						<td width="22%"><a href="index.php?addProduct&<?php echo $pLink; ?>&sortby=hps">Highest Price &amp; Ship</a></td>
						<td width="12%"><a href="index.php?addProduct&<?php echo $pLink; ?>&sortby=gen">Online Only</a></td>
						<td width="12%"><a href="index.php?addProduct&<?php echo $pLink; ?>&sortby=local">Local Only</a></td>
						<td width="10%"><a href="index.php?addProduct&<?php echo $pLink; ?>&sortby=comison">Commission</a></td>										
					</tr>
				</table>
			 
			 <br><table width='100%' border='0' align='center' cellspacing='0'>
					  <tr class='head-tr'>
							  <td width="5%"><strong>S.No</strong></td>								
							  <td width="4%">&nbsp;</td>
							  <td width="17%"> <strong>Product name </strong></td>
							  <td width="12%"><strong>Condition</strong></td>
							  <td width="11%"><strong>Pick up</strong></td>
							  <td width="8%"><strong>Quantity</strong></td>
							  <td width="12%"> <strong> Comission (PP)</strong></td>
							  <td width="18%"> <strong>Price + shipp (PP)</strong></td>
							  <td width="13%"><strong>Feed back</strong></td>
							</tr>				 
					 <?php
					 $r=1;
					 while($rowSelPro=mysql_fetch_array($rstSelPro))
					 {						   
					   $productId=$rowSelPro['product_id'];
					   $productName=wordFormater($rowSelPro['product_name'],12);
					   $productTitle = $rowSelPro['product_name'];					   
					   $productPrice=$rowSelPro['product_price'];
					   $productThumb=$rowSelPro['product_thumbnail'];
					   $productImage=$rowSelPro['product_image'];
					   $productSellingType=$rowSelPro['selling_type'];
					   $productShipping=$rowSelPro['shipping'];
					   $productProCondition=$rowSelPro['pro_condition'];
					   $productQuantity=$rowSelPro['quantity'];
					   $commission=$rowSelPro['commission'];
					   $postedBy=$rowSelPro['posted_by'];
					   $postedDate=$rowSelPro['posted_date'];
					   $bargainDate = $rowSelPro['bargain_duration'];
					   if(!empty($productShipping))
						$productShipping=" + ".$productShipping;
					   else
						 $productShipping=" + Free";
					   $timeLeft = getRemainingTime($bargainDate);
					   $ownerType = getUserType($postedBy);
					   if($ownerType=='1' || $ownerType=='3')
						$ownerType="I";
					   else if($ownerType=='2')
						$ownerType="M";
					   if($timeLeft < 0) {
							$timeLeft = "<font color='red'>Listing Expired</font>";
						}
					  
										   
					   echo "<tr>
							 <td height='28'> $r </td>	
							 <td>$ownerType</td>							         
							 <td> <a href='index.php?SellProduct=$productId' title='$productTitle'> $productName </a> </td>
							 <td>Reburished</td>
							 <td>$productSellingType</td>
							 <td>$productQuantity</td>
							 <td>$commission</td>
							 <td>$productPrice $productShipping</td>
							 <td>99%</td>
						   </tr>";	
						echo "<tr>
								 <td colspan='9'> <div style='border-top:1px solid #CCCCCC;'> </div> </td>
							  </tr>";   			  
						$r++;
					 } ?>
					</table>				


			</td>
		</tr>		
	</table>
<?php } ?>	
	